﻿using AutoMapper;
using MediatR;
using MyRabbit.Application.Command;
using MyRabbit.Application.ErrorCode;
using MyRabbit.Domain;
using MyRabbit.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRabbit.Application.CommandHandler
{
    public class UserAddCommandHandler : IRequestHandler<UserAddCommand, APIResult<object>>
    {

        private readonly IRepository<User> userRep;
        private readonly IMapper mapper; 
        public UserAddCommandHandler(IRepository<User> userRep, IMapper mapper)
        {
            this.userRep = userRep;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(UserAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code=Codes.添加成功;
            result.Message=Codes.添加成功.ToString();


            try
            {
                var entity = mapper.Map<User>(request);
                await userRep.InsertAsync(entity);

            }
            catch (Exception exp)
            { 

                result.Code = Codes.添加成功;
                result.Message = Codes.添加成功.ToString()+exp.Message;
            }




            return await Task.FromResult(result);
        }
    }
}
